import Vue from 'vue'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import App from './App.vue'
import router from './router'
import axios from 'axios' //类似axios.js
// axios请求拦截器给每个请求加上请求头
axios.interceptors.request.use(
  config =>{
// 给请求携带请求头
let token=  localStorage.getItem("authorization")//
if(token){
  config.headers.authorization = token//{headers:{'authorization':token}}
}
let userInfo = localStorage.getItem("userInfo")
    if (userInfo) {
      userInfo = JSON.parse(userInfo)
      config.headers.username = encodeURIComponent(userInfo.account)
      config.headers.roleId = userInfo.roleId.toString()
    }
return config

  }
)

axios.interceptors.response.use(
  resp =>{
    if(resp.data.code==2001){
router.push('/login')  //直接用router而不是this.$router
        }
        if(resp.data.code==500){
        // 跳到一个页面
            }
            return resp
  }
)
Vue.prototype.$axios = axios//把axios配置成全局，所有组件都可以用
Vue.use(ElementUI);
Vue.config.productionTip = false

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
